package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.User;
import com.jt.service.DubboUserService;
import com.jt.util.CookieUtil;
import com.jt.util.ObjectMapperUtil;
import com.jt.vo.SysResult;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/user")
public class UserController {

    @Reference
    private DubboUserService userService;

    /**
     * 实现通用的页面跳转
     * 1). 当用户网址 http://www.jt.com/user/login.html   要求跳转到登录页面中  login.jsp
     * 2). 注册页面  http://www.jt.com/user/register.html  要求跳转到登录页面中  register.jsp
     */
    @RequestMapping("/{moduleName}")
    public String module(@PathVariable String moduleName){
        return moduleName;
    }

    /**
     * 完成用户注册
     * 1.url地址:http://www.jt.com/user/doRegister
     * 2.参数:  password: admin123
     *          username: admin1234
     *          phone: 13111111112
     * 3.返回值:  SysResult对象  json
     */
    @RequestMapping("/doRegister")
    @ResponseBody
    public SysResult doRegister(User user){

        userService.saveUser(user);
        return SysResult.success();
    }

    /**
     * 完成流程用戸单点登录
     * url: http://www.jt.com/user/doLogin?r=0.2677358537241865
     * 返回值：sysResult
     */
    @RequestMapping("/doLogin")
    @ResponseBody
    public SysResult doLogin(User user, HttpServletResponse response){
        String ticket=userService.doLogin(user);
        if (!StringUtils.hasLength(ticket)){
            return SysResult.failed();
        }
//        Cookie cookie = new Cookie("JT_TICKET", ticket);
//        cookie.setDomain("jt.com");
//        cookie.setMaxAge(7*24*60*60);
//        cookie.setPath("/");
//        response.addCookie(cookie);
        CookieUtil.addCookie("JT_TICKET",
                ticket,"jt.com",7*24*60*60,response);
        return SysResult.success();
    }

    /**
     * 实现用户退出操作
     * URL地址:http://www.jt.com/user/logout.html
     * 返回值  重定向到系统首页
     *
     * 通过JT_TICKET获取数据
     * 删除Redis
     * 删除cookie
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request,HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie:cookies){
            String name = cookie.getName();
            if (name.equals("JT_TICKET")){
                CookieUtil.addCookie(cookie.getName(),
                        cookie.getValue(),"jt.com",0,response);
            }
        }
//        for (Cookie cookie:cookies){
//            cookie.setMaxAge(0);
//            response.addCookie(cookie);
//            cookie.getName()
//        }
//        CookieUtil.addCookie(,);
        return "redirect:/";
    }

}















